package com.yutian.selfservice.auth.controller;

import com.yutian.selfservice.auth.annotation.Log;
import com.yutian.selfservice.auth.domain.SysUser;
import com.yutian.selfservice.auth.domain.entity.AjaxResult;
import com.yutian.selfservice.auth.enums.BusinessType;
import com.yutian.selfservice.auth.service.ISysUserService;
import com.yutian.selfservice.auth.shiro.service.SysPasswordService;
import com.yutian.selfservice.auth.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户信息
 * 
 * @author yutian
 */
@Controller
//@RequestMapping("/testuser")
public class SysTestUserController extends BaseController
{
    private String prefix = "system/user";

    @Autowired
    private ISysUserService userService;


    @Autowired
    private SysPasswordService passwordService;


    //@RequiresPermissions("system:user:resetPwd")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwdSave(SysUser user)
    {
        userService.checkUserAllowed(user);
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        if (userService.resetUserPwd(user) > 0)
        {
            if (ShiroUtils.getUserId().longValue() == user.getUserId().longValue())
            {
                setSysUser(userService.selectUserById(user.getUserId()));
            }
            return success();
        }
        return error();
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(SysUser user)
    {

        return userService.checkLoginNameUnique(user.getLoginName());
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(SysUser user)
    {
        return userService.checkPhoneUnique(user);
    }
}